package controladores;

import java.io.Serializable;
import java.util.ArrayList;

import modelo.GestaoAplicacao;

import com.example.rm.teste.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MenuPrincipal extends Activity implements Serializable {
	public static final String estruturasLidas = "estruturasTexto";
	//public static final String estruturasNLidas = "estruturasTexto2";
	public static final String utilizadoresLidos ="utilizadoresTexto";
	public ArrayList<String> utilizadores = new ArrayList<String>();
	ArrayAdapter<String> adapterSpinnerUti;
	
	protected static GestaoAplicacao estruturasLer;
	
	Spinner spinnerU;
	String abd;
	
	String tagRui = this.getClass().getSimpleName()+ "rui";
	
	final Context context = this;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(tagRui,"onCreate");
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_menu);
		
		estruturasLer = new GestaoAplicacao();
		estruturasLer.lerUtilizadoresBD(this);

		utilizadores = estruturasLer.getListasUtilizadoresNomes();

		//estruturasLer.lerRegistoBD(this);
		spinnerU = (Spinner) findViewById(R.id.spinnerMenuUtilizador);
	
		adapterSpinnerUti = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,utilizadores);
		adapterSpinnerUti.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	
		spinnerU.setAdapter(adapterSpinnerUti);
		try {
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
	
	public void onClickAdicionar(View v){
		//Spinner sUtilizadores= (Spinner)findViewById(R.id.spinnerMenuUtilizador);
		
		Intent intent1 = new Intent(getApplicationContext(), AdicionarRegisto.class);
		abd = spinnerU.getSelectedItem().toString();
		intent1.putExtra(utilizadoresLidos, abd);
		startActivity(intent1);
	}
	
	public void onClickHist(View v){
		Intent intent2 = new Intent(getApplicationContext(), Hist.class);
		abd = spinnerU.getSelectedItem().toString();
		intent2.putExtra(estruturasLidas, estruturasLer);
		intent2.putExtra(utilizadoresLidos, abd);
		startActivity(intent2);
	}
	
	public void onClickSobre(View v){
		Intent intent3 = new Intent(getApplicationContext(), Sobre.class);
		startActivity(intent3);
	}
	
	public void onClickDef(View v){
		Intent intent4 = new Intent(getApplicationContext(), Def.class);
		intent4.putExtra(estruturasLidas, estruturasLer);
		startActivity(intent4);
	}
	
	public void onClickEmail(View v){
		Intent intent5 = new Intent(getApplicationContext(), Email.class);
		abd = spinnerU.getSelectedItem().toString();
		intent5.putExtra(estruturasLidas, estruturasLer);
		intent5.putExtra(utilizadoresLidos, abd);
		startActivity(intent5);
	}
	
	public void onClickEst(View v){
		Intent intent223 = new Intent(getApplicationContext(), Estatistica.class);
		startActivity(intent223);
	}

	@Override
	protected void onPause() {
		Log.i(tagRui,"onPause");
		super.onPause();
	}

	@Override
	protected void onResume() {
		Log.i(tagRui,"onResume");
		super.onResume();
	try{
		utilizadores = estruturasLer.getListasUtilizadoresNomes();
		adapterSpinnerUti = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,utilizadores);
		adapterSpinnerUti.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerU.setAdapter(adapterSpinnerUti);
//		adapterSpinnerUti.notifyDataSetChanged();
	}catch(Exception ex){}
	}
}
